<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}"
            @reset="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="子系统" name="type">
            <t-select clearable v-model="queryParams.serviceId" :keys="{label:'serviceName',value:'id'}"
                      style="width: 240px" :options="serviceList"></t-select>
          </t-form-item>
          <t-form-item label="模版类型" name="type">
            <t-select clearable style="width: 240px" placeholder="模版类型" :options="dict.type.msg_template_type"
                      v-model="queryParams.type"></t-select>
          </t-form-item>
          <t-form-item label="是否启用" name="status">
            <t-select clearable style="width: 240px" placeholder="状态" :options="[{label:'启用',value:'1'},{label:'关闭',value:'0'}]"
                      v-model="queryParams.status"></t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增短信模版';dialog.objId=0;dialog.open = true;" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增
      </t-button>
    </div>
    <t-tabs  @change="(val)=>{queryParams.serviceId=val;getList();}" v-model="queryParams.serviceId" style="margin-bottom: 10px">
      <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in [...serviceList]" :label="item.serviceName" :destroyOnHide="false">
      </t-tab-panel>
    </t-tabs>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #sort="{rowIndex}">
       {{ rowIndex+1 }}
      </template>
      <template #type="{row}">
        {{row.type | filterByDict(dict.type.msg_template_type)}}
      </template>
      <template #status="{row}">
        <t-tag theme="success" variant="light" v-if="row.status==='1'">
          启用
        </t-tag>
        <t-tag theme="danger" variant="light" v-else-if="row.status==='0'">
          关闭
        </t-tag>
      </template>
      <template #op="{ row }">
        <t-button @click="dialog.header='修改短信模版';dialog.objId=row.id;dialog.open = true;" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button @click="delObj(row)" theme="danger" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-table>
    <add-edit-template @reload="getList()" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open" v-if="dialog.open"></add-edit-template>
  </div>
</template>

<script>
import addEditTemplate from "./components/add-edit-template.vue";
import { sysServiceBusinessList } from "@/api/system/role";

export default {
  name: "template-index",
  dicts: ['msg_template_type'],
  components:{
    addEditTemplate
  },
  data(){
    return {
      dialog:{
        header: '',
        objId: null,
        open: false
      },
      data: [],
      columns: [
        {
          colKey: "sort",
          title: "序号",
          align: "center",
          width: 64
        },
        {
          colKey: "type",
          title: "模版类型",
          align: "center",
        },
        {
          colKey: "content",
          title: "模版内容",
          align: "center",
        },
        // {
        //   colKey: "note",
        //   title: "备注",
        //   align: "center",
        // },
        {
          colKey: "status",
          title: "是否启用",
          align: "center",
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 100
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        type: '',
        status: '1',
        serviceId: null,
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      serviceList: []
    }
  },
  created(){
    this.getServiceList();
  },
  methods:{
    getServiceList() {
      sysServiceBusinessList().then((res) => {
        this.serviceList = res.data;
        if(this.serviceList.length>0){
          this.queryParams.serviceId = this.serviceList[0].id;
        }
        this.getList();
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getList(){
      this.dataLoading = true;
      this.$api.system.sms.templateList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除模版内容为"${row.content}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          this.$api.system.sms.templateDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
